package services;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import domain.RaumBelegung;
import domain.Studiengang;
import domain.Universitaet;

/**
 * Interface fuer das schreiben in das Dateisystem als CSV-Datei
 * 
 * @author Patrick Nielinger
 * @version 14.12.14
 */
public class CSVOutputService implements OutputService {

	private Universitaet universitaet;
	private ConfigurationService configurationService;
	private String dozentenPlaeneOutputName;
	private String raumPlaeneOutputName;
	private String studiengangPlaeneOutputName;
	private BusinessServicesProvider bsp;

	/**
	 * Constructor for objects of class CSVOutputService
	 * @throws IOException 
	 */
	public CSVOutputService(Universitaet universitaet,
			ConfigurationService configurationService, BusinessServicesProvider bsp) throws IOException {
		this.universitaet = universitaet;
		this.configurationService = configurationService;
		this.bsp = bsp;
		
		Properties config = configurationService.getPropValues();
		dozentenPlaeneOutputName = config.getProperty("dozentenPlaeneOutputName").toString();
		raumPlaeneOutputName = config.getProperty("raumPlaeneOutputName").toString();
		studiengangPlaeneOutputName = config.getProperty("studiengangPlaeneOutputName").toString();
	}
	
	public void schreibeRaumPlaene() {
		try {
			File newFile = new File("C:\\srv\\" + raumPlaeneOutputName + ".csv");
			newFile.getParentFile().mkdirs();
			
			FileWriter writer = new FileWriter(newFile, false);

			writer.append("Raum");
			writer.append(';');
			writer.append("Lehrveranstaltung");
			writer.append(';');
			writer.append("Dozent");
			writer.append(';');
			writer.append("Tag");
			writer.append(';');
			writer.append("Von");
			writer.append(';');
			writer.append("Bis");
			writer.append('\n');
			
			List<RaumBelegung> rbs = universitaet.getRaumbelegungen();
			Collections.sort(rbs, (p1, p2) -> p1.getRaum().getName().compareTo(p2.getRaum().getName()));

			for(RaumBelegung rb : rbs){
				String tag = new SimpleDateFormat("EEEE", Locale.GERMAN).format(rb.getVon());
				String von = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getVon());
		        String bis = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getBis());
				
				writer.append(rb.getRaum().getName());
				writer.append(';');
				writer.append(rb.getLehrveranstaltung().getName());
				writer.append(';');
				writer.append(rb.getDozent().getName());
				writer.append(';');
				writer.append(tag);
				writer.append(';');
				writer.append(von);
				writer.append(';');
				writer.append(bis);
				writer.append('\n');
			}

			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void schreibeStudiengangPlaene() throws Exception {
		try{
		File newFile = new File("C:\\srv\\" + studiengangPlaeneOutputName + ".csv");
		newFile.getParentFile().mkdirs();
		
		FileWriter writer = new FileWriter(newFile, false);

		writer.append("Studiengang");
		writer.append(';');
		writer.append("Lehrveranstaltung");
		writer.append(';');
		writer.append("Semester");
		writer.append(';');
		writer.append("Raum");
		writer.append(';');
		writer.append("Dozent");
		writer.append(';');
		writer.append("Tag");
		writer.append(';');
		writer.append("Von");
		writer.append(';');
		writer.append("Bis");
		writer.append('\n');
		
		List<RaumBelegung> rbs = null;
		
		List<Studiengang> stds = universitaet.getStudiengaenge();
		
		for(Studiengang std : stds){
			rbs = bsp.getRaumbelegungByStudiengangName(std.getName());
		
			for(RaumBelegung rb : rbs){
				String tag = new SimpleDateFormat("EEEE", Locale.GERMAN).format(rb.getVon());
				String von = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getVon());
		        String bis = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getBis());
		        
		        int moduleSemester = std.getModuleSemester(rb.getLehrveranstaltung().getName());
		        
		        writer.append(std.getName());
				writer.append(';');
				writer.append(rb.getLehrveranstaltung().getName());
				writer.append(';');
				writer.append(String.valueOf(moduleSemester));
				writer.append(';');
				writer.append(rb.getRaum().getName());
				writer.append(';');
				writer.append(rb.getDozent().getName());
				writer.append(';');
				writer.append(tag);
				writer.append(';');
				writer.append(von);
				writer.append(';');
				writer.append(bis);
				writer.append('\n');
			}
		}

		writer.flush();
		writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void schreibeDozentenPlaene() throws Exception{
		try {
			File newFile = new File("C:\\srv\\" + dozentenPlaeneOutputName + ".csv");
			newFile.getParentFile().mkdirs();
			
			FileWriter writer = new FileWriter(newFile, false);

			writer.append("Dozent");
			writer.append(';');
			writer.append("Raum");
			writer.append(';');
			writer.append("Lehrveranstaltung");
			writer.append(';');
			writer.append("Tag");
			writer.append(';');
			writer.append("Von");
			writer.append(';');
			writer.append("Bis");
			writer.append('\n');
			
			List<RaumBelegung> rbs = universitaet.getRaumbelegungen();
			Collections.sort(rbs, (p1, p2) -> p1.getDozent().getName().compareTo(p2.getDozent().getName()));

			for(RaumBelegung rb : rbs){
				String tag = new SimpleDateFormat("EEEE", Locale.GERMAN).format(rb.getVon());
				String von = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getVon());
		        String bis = new SimpleDateFormat("HH:mm", Locale.GERMAN).format(rb.getBis());
				
				writer.append(rb.getDozent().getName());
				writer.append(';');
				writer.append(rb.getRaum().getName());
				writer.append(';');
				writer.append(rb.getLehrveranstaltung().getName());
				writer.append(';');
				writer.append(tag);
				writer.append(';');
				writer.append(von);
				writer.append(';');
				writer.append(bis);
				writer.append('\n');
			}

			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
